//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using CommunityServer.Components;
using CommunityServer.Files.Components;
using System.Collections;
using System.Text;

namespace CommunityServer.SqlDataProvider
{
	/// <summary>
	/// Summary description for FileThreadQueryBuild.
	/// </summary>
	public class FileThreadQueryBuilder : BaseThreadQueryBuilder
	{
		private FileGalleryThreadQuery query;
		public FileThreadQueryBuilder(ThreadQuery query, string databaseOwner): base(query, databaseOwner)
		{
			this.query = (FileGalleryThreadQuery)query;
		}

		#region QueryBulder Overrides

		/// <summary>
		/// ORDER BY, HAVING and GROUP BY objects appended after the WHERE clause
		/// </summary>
		protected override void ApplySort()
		{
			string order = "desc";
			if(query.SortOrder == SortOrder.Ascending)
				order = "asc";

			// Sort by
			string groupBy = string.Empty;
			string orderBy = string.Empty;
			switch(query.SortBy)
			{
				case EntriesSortBy.Author:
					orderBy = " order by P.PostAuthor";
					groupBy = "P.PostAuthor";
					break;
				case EntriesSortBy.Comments:
					orderBy = " order by T.TotalReplies";
					groupBy = "T.TotalReplies";
					break;
				case EntriesSortBy.Rating:
					orderBy = " order by case when T.TotalRatings > 0 then (convert(decimal,T.RatingSum) / convert(decimal,T.TotalRatings)) else 0 end" ;
					groupBy = "T.TotalRatings, T.RatingSum";
					break;
				case EntriesSortBy.Subject:
					orderBy = " order by P.Subject";
					groupBy = "P.Subject";
					break;
				case EntriesSortBy.Views:
					orderBy = " order by T.TotalViews";
					groupBy = "T.TotalViews";
					break;
				case EntriesSortBy.Downloads:
					orderBy = " order by (SELECT COUNT(fD.UserID) FROM files_Downloads fD (nolock) WHERE fD.PostID = P.PostID)";
					break;
				default:
				case EntriesSortBy.PostDate:
					orderBy = " order by P.PostDate";
					groupBy = "P.PostDate";
					break;
			}

			// Add group by and having if necessary, then order by
			if(query.UncategorizedOnly)
			{
				sb.Append(" group by P.PostID, " + groupBy);
				sb.Append(" having count(PC.CategoryID) = 0");
			}
			sb.Append(orderBy + " " + order);
		}

		/// <summary>
		/// WHERE clause element to specify the valid IsApproved / PostDate values
		/// </summary>
		protected override void ApplyPublished()
		{
			//Do we care if the post has been publshied
			if(query.OnlyApproved)
			{
				sb.Append(" and P.IsApproved = 1 and P.PostDate <= getdate() ");
			}
			else if (query.OnlyUnapproved)
			{
				sb.Append(" and P.IsApproved = 0 and P.PostDate <= getdate() ");
			}
		}

		/// <summary>
		/// WHERE clause element to speficy the ApplicationPostType to return (bitwise comparison)
		/// </summary>
		protected override void ApplyPostType()
		{
			sb.AppendFormat(" and P.ApplicationPostType & {0} <> 0 ", (int) query.ApplicationPostType);
			//sb.AppendFormat(" and P.ApplicationPostType = {0} ", (int)query.PostType);
		}

		/// <summary>
		/// WHERE clause elemenet to apply values set in the FilterKey
		/// The base only runs this if the key length is greater than zero
		/// </summary>
		protected override void ApplyFilterKey()
		{
			sb.AppendFormat(" and S.SectionID in ({0}) ", query.FilterKey);
		}

		protected override string GetSectionIDList()
		{
			ArrayList folders = Folders.GetFolders();
			if (folders.Count > 0)
			{
				StringBuilder list = new StringBuilder();
				foreach (Folder f in folders)
				{
					if (list.Length > 0)
						list.Append(",");

					list.Append(f.SectionID);
				}

				return list.ToString();
			}
			else
				return "null";
		}


		#endregion
	}
}
